package com.kd.crawler.common.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

public class StringUtils {
	public static int getInt(String value){
		if(value==null||value.trim().equals("")){
			value="0";
		}
		return Integer.valueOf(value.trim()).intValue();
	}
	public static int getByte(String value){
		if(value==null||value.trim().equals("")){
			value="0";
		}
		return Integer.valueOf(value).intValue();
	}
	public static String toString(String array[], String split) {
		if(array==null){
			return "";
		}
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < array.length; i++) {
			buf.append(split+array[i]);
		}
		return buf.toString().length()>0?buf.toString().substring(1):buf.toString();
	}
	public static String isoToGB2312(String str){
		if(str==null){
			return null;
		}
		try {
			return new String(str.getBytes("iso8859_1"),"GB2312");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public static String isoToUTF8(String str){
		if(str==null){
			return null;
		}
		try {
			return new String(new String(str.getBytes("iso8859_1"),"GB2312").getBytes("GB2312"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	public static String toString(Exception e){
		StringWriter sw=new StringWriter();
		e.printStackTrace(new PrintWriter(sw));
		return sw.getBuffer().toString();
	}
	public static String toString(int intArray[],String splitStr){
		if(intArray==null||intArray.length<=0){
			return null;
		}
		StringBuffer buf=new StringBuffer();
		for(int i:intArray){
			buf.append(splitStr+i);
		}
		return buf.toString().substring(splitStr.length());
	}
	
	public static boolean isBlank(String str) {
		if(null==str || "".equals(str)) {
			return true;
		}
		
		return false;
	}
	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}
	
	
	/**
	 * fetch a substring between startStr and endStr.
	 * @param str
	 * @param startStr
	 * @param endStr
	 * @return
	 */
	public static String substringBetween(String str, String startStr, String endStr) {
		if(null==str || "".equals(str)) {
			return null;
		}
		int startIndex = 0;
		int endIndex = str.length();
		if(null==startStr || "".equals(startStr)) {
			startIndex = 0;
		} else {
			startIndex = str.indexOf(startStr);
		}
		if(-1 == startIndex) {
			return null;
		}
		if(null== endStr || "".equals(endStr)) {
			endIndex = str.length();
		} else {
			if(0 != startIndex) {
				endIndex = str.indexOf(endStr, startIndex+startStr.length());
			} else {
				endIndex = str.indexOf(endStr);
			}
		}
		if(-1 == endIndex) {
			return null;
		} else {
			endIndex += 1;
		}
		return str.substring(startIndex, endIndex);
	}
	
	public static String substringBeforeLast(String str, String s) {
		if(null==str || "".equals(str)) {
			return null;
		}
		if(null==s || "".equals(s)) {
			return str;
		}
		return str.substring(0, str.lastIndexOf(s));
	}
	
	public static String substringBefore(String string, String separator,	int charCount) {
		int indexOf = 0;
		for (int i = 0; i < charCount; i++) {

			indexOf = string.indexOf(separator,indexOf+1);
		}
		if (indexOf>=0 ){
			return string.substring(0, indexOf);
		}
		return null;
	}
	
	public static boolean isAscii(String str) {
		if (str == null) {
			return true;
		}
		int sz = str.length();
		for (int i = 0; i < sz; i++)  {
			if (str.charAt(i) >= 128) {
				return false;
			}
		}
		return true;
	}
	
	public static int indexOf(String str, String searchStr, int startPos) {
		if(null==str || str.isEmpty()) {
			return -1;
		}
		return str.indexOf(searchStr, startPos);
	}
	public static int indexOf(String str, char searchChar, int startPos) {
		if(null==str || str.isEmpty()) {
			return -1;
		}
		return str.indexOf(searchChar, startPos);
	}
	
	public static String trimSpace(String str) {
		if(isBlank(str)) {
			return null;
		}
		return str.trim();
	}
}
